package com.umer.pinban.datafenxi;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.okhttp.OkEntityRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.loadsir.callback.EmptyCallback;
import com.common.ui.loadsir.callback.ErrorCallback;
import com.common.ui.loadsir.callback.NoNetCallback;
import com.common.ui.recycleview.decoration.DividerGridItemDecoration;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.common.utils.Constants;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.bean.fenxi.CardValidTypeListBean;
import com.umer.pinban.bean.fenxi.CustomerTypeListBean;
import com.umer.pinban.bean.fenxi.ExpenseTypeListBean;
import com.umer.pinban.bean.fenxi.ExperiencePayTypeListBean;
import com.umer.pinban.bean.fenxi.ExperienceTypeListBean;
import com.umer.pinban.bean.fenxi.FenXiDataBean;
import com.umer.pinban.bean.fenxi.IntoShopTypeListBean;
import com.umer.pinban.bean.fenxi.ProductAnalyzeListBean;
import com.umer.pinban.bean.fenxi.ProjectAnalyzeListBean;
import com.umer.pinban.bean.fenxi.ProjectExpenseListBean;
import com.umer.pinban.bean.staff.StaffBean;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterEight;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterFive;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterFour;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterNine;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterOne;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterSeven;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterSix;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterThree;
import com.umer.pinban.datafenxi.adapter.DataFenXiAdapterTwo;
import com.umer.pinban.datafenxi.adapter.StaffDataFenXiAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 数据分析
 */
public class DataFenXiActivity extends BaseActivity implements OptListener, DataStateListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.linear_old_data)
    LinearLayout linearOldData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.view_total)
    View viewTotal;
    @BindView(R.id.linear_total)
    LinearLayout linearTotal;
    @BindView(R.id.view_month)
    View viewMonth;
    @BindView(R.id.linear_month)
    LinearLayout linearMonth;
    @BindView(R.id.tv_vip_numbers)
    TextView tvVipNumbers;
    @BindView(R.id.recyclerView_one_one)
    RecyclerView recyclerViewOneOne;
    @BindView(R.id.tv_jindjian_numbers)
    TextView tvJindjianNumbers;
    @BindView(R.id.recyclerView_one_two)
    RecyclerView recyclerViewOneTwo;
    @BindView(R.id.tv_xiaofei_money)
    TextView tvXiaofeiMoney;
    @BindView(R.id.recyclerView_one_three)
    RecyclerView recyclerViewOneThree;
    @BindView(R.id.tv_qiankuan)
    TextView tvQiankuan;
    @BindView(R.id.linear_qiankuan)
    LinearLayout linearQiankuan;
    @BindView(R.id.tv_customer_weihu)
    TextView tvCustomerWeihu;
    @BindView(R.id.linear_customer_weihu)
    LinearLayout linearCustomerWeihu;
    @BindView(R.id.tv_staff_xinke_tiyan)
    TextView tvStaffXinkeTiyan;
    @BindView(R.id.recyclerView_two_one)
    RecyclerView recyclerViewTwoOne;
    @BindView(R.id.tv_staff_xinke_chengjiao)
    TextView tvStaffXinkeChengjiao;
    @BindView(R.id.recyclerView_two_two)
    RecyclerView recyclerViewTwoTwo;
    @BindView(R.id.tv_staff_yeji)
    TextView tvStaffYeji;
    @BindView(R.id.linear_staff_yeji)
    LinearLayout linearStaffYeji;
    @BindView(R.id.tv_staff_shougong)
    TextView tvStaffShougong;
    @BindView(R.id.linear_staff_shougong)
    LinearLayout linearStaffShougong;
    @BindView(R.id.recyclerView_three_one)
    RecyclerView recyclerViewThreeOne;
    @BindView(R.id.recyclerView_three_two)
    RecyclerView recyclerViewThreeTwo;
    @BindView(R.id.recyclerView_four_one)
    RecyclerView recyclerViewFourOne;
    @BindView(R.id.recyclerView_four_two)
    RecyclerView recyclerViewFourTwo;
    @BindView(R.id.tv_price_one)
    TextView tvPriceOne;
    @BindView(R.id.tv_price_two)
    TextView tvPriceTwo;
    private UserInfo userInfo;
    //美容师
    private List<StaffBean> staffBeans = new ArrayList<>();
    private StaffDataFenXiAdapter staffDataFenXiAdapter;
    private int staffID = 0;//美容师id
    private int index = 0;// 0:总 1:月
    private int yearTime = 0;
    private int monthTime = 0;
    private FenXiDataBean fenXiDataBean;

    private DataFenXiAdapterOne dataFenXiAdapterOne;
    private List<CustomerTypeListBean> customerTypeList = new ArrayList<>();

    private DataFenXiAdapterTwo dataFenXiAdapterTwo;
    private List<IntoShopTypeListBean> intoShopTypeListBeans = new ArrayList<>();

    private DataFenXiAdapterThree dataFenXiAdapterThree;
    private List<ExpenseTypeListBean> expenseTypeListBeans = new ArrayList<>();


    //员工
    private DataFenXiAdapterFour dataFenXiAdapterFour;
    private List<ExperienceTypeListBean> experienceTypeListBeans = new ArrayList<>();

    private DataFenXiAdapterFive dataFenXiAdapterFive;
    private List<ExperiencePayTypeListBean> experiencePayTypeListBeans = new ArrayList<>();

    //项目和产品分析

    private DataFenXiAdapterSix dataFenXiAdapterSix;
    private List<ProjectAnalyzeListBean> projectAnalyzeListBeans = new ArrayList<>();

    private DataFenXiAdapterSeven dataFenXiAdapterSeven;
    private List<ProductAnalyzeListBean> productAnalyzeListBeans = new ArrayList<>();

    //项目消耗
    private DataFenXiAdapterEight dataFenXiAdapterEight;
    private List<ProjectExpenseListBean> projectExpenseListBeans = new ArrayList<>();
    private DataFenXiAdapterNine dataFenXiAdapterNine;
    private List<CardValidTypeListBean> cardValidTypeListBeans = new ArrayList<>();


    @Override
    public View isSupportLoadSirEnabled() {
        return llContent;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_data_fenxi;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        StaffBean staffBean = new StaffBean();
        staffBean.setName("全部");
        staffBean.setId(0);
        staffBean.setSelect(true);
        staffBeans.add(staffBean);
        staffDataFenXiAdapter = new StaffDataFenXiAdapter(this, staffBeans, R.layout.item_staff_one, this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 10);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        recyclerView.setAdapter(staffDataFenXiAdapter);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        //第一行
        dataFenXiAdapterOne = new DataFenXiAdapterOne(this, customerTypeList, R.layout.item_fenxi_one, this);
        LinearLayoutManager linearLayoutManagerOne = new LinearLayoutManager(this);
        recyclerViewOneOne.setLayoutManager(linearLayoutManagerOne);
        recyclerViewOneOne.setAdapter(dataFenXiAdapterOne);

        dataFenXiAdapterTwo = new DataFenXiAdapterTwo(this, intoShopTypeListBeans, R.layout.item_fenxi_two, this);
        LinearLayoutManager linearLayoutManagerTwo = new LinearLayoutManager(this);
        recyclerViewOneTwo.setLayoutManager(linearLayoutManagerTwo);
        recyclerViewOneTwo.setAdapter(dataFenXiAdapterTwo);

        dataFenXiAdapterThree = new DataFenXiAdapterThree(this, expenseTypeListBeans,
                R.layout.item_fenxi_three, this);
        LinearLayoutManager linearLayoutManagerThree = new LinearLayoutManager(this);
        recyclerViewOneThree.setLayoutManager(linearLayoutManagerThree);
        recyclerViewOneThree.setAdapter(dataFenXiAdapterThree);

        //第一行 员工
        dataFenXiAdapterFour = new DataFenXiAdapterFour(this, experienceTypeListBeans,
                R.layout.item_fenxi_four, this);
        LinearLayoutManager linearLayoutManager4 = new LinearLayoutManager(this);
        recyclerViewTwoOne.setLayoutManager(linearLayoutManager4);
        recyclerViewTwoOne.setAdapter(dataFenXiAdapterFour);

        dataFenXiAdapterFive = new DataFenXiAdapterFive(this, experiencePayTypeListBeans,
                R.layout.item_fenxi_five, this);
        LinearLayoutManager linearLayoutManager5 = new LinearLayoutManager(this);
        recyclerViewTwoTwo.setLayoutManager(linearLayoutManager5);
        recyclerViewTwoTwo.setAdapter(dataFenXiAdapterFive);

        //项目和产品
        dataFenXiAdapterSix = new DataFenXiAdapterSix(this, projectAnalyzeListBeans,
                R.layout.item_fenxi_six, this);
        LinearLayoutManager linearLayoutManager6 = new LinearLayoutManager(this);
        recyclerViewThreeOne.setLayoutManager(linearLayoutManager6);
        recyclerViewThreeOne.setAdapter(dataFenXiAdapterSix);

        dataFenXiAdapterSeven = new DataFenXiAdapterSeven(this, productAnalyzeListBeans,
                R.layout.item_fenxi_seven, this);
        LinearLayoutManager linearLayoutManager7 = new LinearLayoutManager(this);
        recyclerViewThreeTwo.setLayoutManager(linearLayoutManager7);
        recyclerViewThreeTwo.setAdapter(dataFenXiAdapterSeven);

        //项目消耗
        dataFenXiAdapterEight = new DataFenXiAdapterEight(this, projectExpenseListBeans,
                R.layout.item_fenxi_eight, this);
        LinearLayoutManager linearLayoutManager8 = new LinearLayoutManager(this);
        recyclerViewFourOne.setLayoutManager(linearLayoutManager8);
        recyclerViewFourOne.setAdapter(dataFenXiAdapterEight);

        dataFenXiAdapterNine = new DataFenXiAdapterNine(this, cardValidTypeListBeans,
                R.layout.item_fenxi_nine, this);
        LinearLayoutManager linearLayoutManager9 = new LinearLayoutManager(this);
        recyclerViewFourTwo.setLayoutManager(linearLayoutManager9);
        recyclerViewFourTwo.setAdapter(dataFenXiAdapterNine);

    }

    @Override
    public void loadData() {
        selectStaffList();
    }

    @OnClick({R.id.linear_back, R.id.linear_old_data, R.id.linear_total, R.id.linear_month, R.id.linear_qiankuan,
            R.id.linear_customer_weihu, R.id.linear_staff_yeji, R.id.linear_staff_shougong})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.linear_old_data:
                break;
            case R.id.linear_total:
                if (index == 0) {
                    return;
                } else {
                    index = 1;
                    viewTotal.setVisibility(View.VISIBLE);
                    viewMonth.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.linear_month:
                if (index == 1) {
                    return;
                } else {
                    if (monthTime == 0 || yearTime == 0) {
                        yearTime = getYear();
                        monthTime = getMonth();
                    }
                    index = 0;
                    viewTotal.setVisibility(View.INVISIBLE);
                    viewMonth.setVisibility(View.VISIBLE);
                    dataAnalyze();
                }
                break;
            //欠款详情
            case R.id.linear_qiankuan:
                break;
            //客户维护详情
            case R.id.linear_customer_weihu:
                break;


            //员工部分
            //业绩
            case R.id.linear_staff_yeji:
                break;
            //手工
            case R.id.linear_staff_shougong:
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        int position = (int) object;
        switch (view.getId()) {
            case R.id.tv:
                boolean select = staffDataFenXiAdapter.getDataSource().get(position).isSelect();
                if (select) {
                    return;
                } else {
                    staffID = staffDataFenXiAdapter.getDataSource().get(position).getId();
                    staffDataFenXiAdapter.getDataSource().get(position).setSelect(true);
                    for (int i = 0; i < staffDataFenXiAdapter.getDataSource().size(); i++) {
                        if (i != position) {
                            staffDataFenXiAdapter.getDataSource().get(i).setSelect(false);
                        }
                    }
                    staffDataFenXiAdapter.notifyDataSetChanged();
                }
                break;
            //会员总数
            case R.id.linear_one:
                break;
            //进店量
            case R.id.linear_two:
                break;
            //消费
            case R.id.linear_three:
                break;

            //员工部分
            //体验
            case R.id.linear_four:
                break;
            //成交
            case R.id.linear_five:
                break;
            //项目分析
            case R.id.linear_six:
                break;
            //产品分析
            case R.id.linear_seven:
                break;
            default:
                break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.personnelList:
                List<StaffBean> beans = (List<StaffBean>) t.getT();
                if (beans != null && beans.size() > 0) {
                    staffBeans.addAll(beans);
                    staffDataFenXiAdapter.setDataSource(staffBeans);
                    dataAnalyze();
                }
                break;
            case R.id.dataAnalyze:
                fenXiDataBean = (FenXiDataBean) t.getT();
                if (fenXiDataBean != null) {
                    if (loadService != null) {
                        loadService.showSuccess();
                    }
                    putData(fenXiDataBean);
                } else {
                    if (loadService != null) {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.personnelList:
                break;
            case R.id.dataAnalyze:
                if (loadService != null) {
                    loadService.showCallback(ErrorCallback.class);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onDataState(int flag) {
        //1:没有网络 2:没有数据 3:获取数据失败 4:显示内容
        switch (flag) {
            case 1:
                if (loadService != null) {
                    loadService.showCallback(NoNetCallback.class);
                }
                break;
            case 2:
                if (loadService != null) {
                    loadService.showCallback(EmptyCallback.class);
                }
                break;
            case 3:
                if (loadService != null) {
                    loadService.showCallback(ErrorCallback.class);
                }
                break;
            case 4:
                if (loadService != null) {
                    loadService.showSuccess();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 获取年
     *
     * @return
     */
    public int getYear() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.YEAR);
    }

    /**
     * 获取月
     *
     * @return
     */
    public int getMonth() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MONTH) + 1;
    }

    private void selectStaffList() {
        OkEntityListRequest<StaffBean> request = new OkEntityListRequest<>(R.id.personnelList,
                Constants.personnelList, StaffBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void dataAnalyze() {
        OkEntityRequest<FenXiDataBean> request = new OkEntityRequest<>(R.id.dataAnalyze,
                Constants.dataAnalyze, FenXiDataBean.class);
        request.addParams("shopId", userInfo.getShopId());
        if (staffID != 0) {
            request.addParams("personnelId", staffID);
        }

        if (yearTime != 0) {
            request.addParams("yearTime", yearTime);
        }
        if (monthTime != 0) {
            request.addParams("monthTime", monthTime);
        }
        request.setHeader(true);
        requestOkhttpEntity(request);
    }

    private void putData(FenXiDataBean fenXiDataBean) {

        //第一部分
        customerTypeList = fenXiDataBean.getCustomerTypeList();
        intoShopTypeListBeans = fenXiDataBean.getIntoShopTypeList();
        expenseTypeListBeans = fenXiDataBean.getExpenseTypeList();
        //第二部分 员工
        experienceTypeListBeans = fenXiDataBean.getExperienceTypeList();
        experiencePayTypeListBeans = fenXiDataBean.getExperiencePayTypeList();
        //项目和产品
        projectAnalyzeListBeans = fenXiDataBean.getProjectAnalyzeList();
        productAnalyzeListBeans = fenXiDataBean.getProductAnalyzeList();
        //项目消耗
        projectExpenseListBeans = fenXiDataBean.getProjectExpenseList();
        cardValidTypeListBeans = fenXiDataBean.getCardValidTypeList();


        tvVipNumbers.setText("" + fenXiDataBean.getCustomerNum());
        if (customerTypeList != null && customerTypeList.size() > 0) {
            dataFenXiAdapterOne.setDataSource(customerTypeList);
        }

        tvJindjianNumbers.setText("" + fenXiDataBean.getIntoShopNum());
        if (intoShopTypeListBeans != null && intoShopTypeListBeans.size() > 0) {
            dataFenXiAdapterTwo.setDataSource(intoShopTypeListBeans);
        }

        tvXiaofeiMoney.setText("" + fenXiDataBean.getExpensePrice());
        if (expenseTypeListBeans != null && expenseTypeListBeans.size() > 0) {
            dataFenXiAdapterThree.setDataSource(expenseTypeListBeans);
        }

        tvQiankuan.setText("" + fenXiDataBean.getDebtPrice());
        tvCustomerWeihu.setText("" + fenXiDataBean.getRemarkNum());

        //第二部分
        tvStaffXinkeTiyan.setText("" + fenXiDataBean.getExperienceNum());
        if (experienceTypeListBeans != null && experienceTypeListBeans.size() > 0) {
            dataFenXiAdapterFour.setDataSource(experienceTypeListBeans);
        }

        tvStaffXinkeChengjiao.setText("" + fenXiDataBean.getExperiencePayNum());
        if (experiencePayTypeListBeans != null && experiencePayTypeListBeans.size() > 0) {
            dataFenXiAdapterFive.setDataSource(experiencePayTypeListBeans);
        }

        tvStaffYeji.setText("" + fenXiDataBean.getExpensePrice());
        tvStaffShougong.setText("" + fenXiDataBean.getExperiencePayNum());


        //第三部分 项目和产品
        if (projectAnalyzeListBeans != null && projectAnalyzeListBeans.size() > 0) {
            dataFenXiAdapterSix.setDataSource(projectAnalyzeListBeans);
        }
        if (productAnalyzeListBeans != null && productAnalyzeListBeans.size() > 0) {
            dataFenXiAdapterSeven.setDataSource(productAnalyzeListBeans);
        }

        //第四部分
        if (projectExpenseListBeans != null && projectExpenseListBeans.size() > 0) {
            dataFenXiAdapterEight.setDataSource(projectExpenseListBeans);
        }
        if (cardValidTypeListBeans != null && cardValidTypeListBeans.size() > 0) {
            dataFenXiAdapterNine.setDataSource(cardValidTypeListBeans);
        }
        tvPriceOne.setText(fenXiDataBean.getExpensePrice());
        tvPriceTwo.setText(fenXiDataBean.getIncomePrice());
    }
}
